<?php
/**
 * 产品提货
 * User: bgl
 * Date: 2023/2/2
 * Time: 18:17
 */

namespace App\Admin\Controllers\ProductPickup;


use App\Admin\Ajax\ProductPickup\PickupAjax;
use App\Admin\Extensions\Grid\Tool\Export;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Models\ProductPickup\Pickup;
use App\Models\ProductPickup\PickupOrderLog;
use App\Models\ProductStockup\Stockup;
use App\Models\SsBasic\StockupAreaSkuEstimate;
use App\Util\User;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Request;

class PickupController extends AdminController{

    protected $title = '提货汇总（新）';

    protected function grid(){

        Admin::style($this->_style());
        $grid = new Grid(new Pickup());
        $grid->model()->groupBy('classify_id')->orderByDesc('id');

        $grid->column('classify.title','品类')->expand(function ($model) use($grid){
            $classifySku = Pickup::getClassifySku($model->classify_id);
            return view('product-pickup.pickup.index', compact('classifySku'));
        })->style(' border-bottom: 0px solid #f4f4f4;')->totalRow('合计');

        /* $grid->column('classify.season','季节性')->display(function ($value){
             return '';
         });
         $grid->column('sku.psku_code','产品SKU')->display(function ($value){
             return '';
         });
         $grid->column('sku.m_psku_id','主SKU'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')->display(function ($value){
             return '';
         });
         $grid->column('sku.psku_cn','SKU名称')->display(function ($value){
             return '';
         })->style('width: 350px; display: block; padding-top: 15px; border-bottom: 0px solid #f4f4f4;');
         $grid->column('cn_instant_num','在库数量')->display(function (){
             return Pickup::query()->where('classify_id', $this->classify_id)->sum('instant_num');

         });
         $grid->column('cn_transit_num','在途数量')->display(function (){
             return Pickup::query()->where('classify_id', $this->classify_id)->sum('transit_num');
         });
         $grid->column('instant_num1','在库数量1')->display(function (){
             $pskuIds = Pickup::query()->where('classify_id', $this->sku->m_psku_id)->sum('instant_num');
             return $pskuIds;
         });
         $grid->column('transit_num1','在途数量1')->display(function (){
             return Pickup::query()->where('psku_id', $this->sku->m_psku_id)->sum('transit_num');
         });

         for($i = 0; $i < StockupAreaSkuEstimate::NEW_ESTIMATE_NUM; $i++){
             $grid->column('pickup'.$i, date('y/m',strtotime("midnight first day of $i month")))->display(function ($value){
             })->style('width: 100%; max-width: 300px;')->help('<br/>提货计划数量,<br/>实时计划数量,<br/>发货数量,<br/>发货箱数');
         }
         $grid->column('seller_id','操作人')->display(function ($value){
             return \App\Util\User::getUsernameById($value);
         });
         $grid->column('diff_num','差异数')->display(function ($value){
             $total = 0;
             return $total;
         });*/

        $grid->tools(function (Grid\Tools $tools){
            //$tools->append(new Export('stockup/stockup-download'));
        });
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->expandFilter();
        $grid->paginate(10);

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
            });
        });
        return $grid;
    }

    public function modal(){
        $action = request('action');
        $param = request('param');
        $modal = new PickupAjax();
        return $param ? call_user_func([$modal,$action],$param) : call_user_func([$modal,$action]);
    }

    protected function _style(){

return <<<STYLE
    .table > thead > tr > th, .table > tbody > tr > td {
        white-space: nowrap;
    }
    .table-responsive {
        max-height: calc(80vh);
    }

    table thead{
        z-index: 2;
        position: sticky;
        top: 0;
        left: 0;
        background: #fff;
    }

STYLE;
    }

}


